﻿Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Services.Localization

Namespace effority.WF_HTML

Partial Class WF_Grid
    Inherits System.Web.UI.UserControl

#Region " Private Members "

        Private _userlevel As effority.WF_HTML.UserLevelEnum
        Private _ModuleId As Int32

        Private _localResourceFile As String

#End Region

#Region " Public Properties "

        Public Property UserLevel() As effority.WF_HTML.UserLevelEnum
            Get
                Return _userlevel
            End Get
            Set(ByVal Value As effority.WF_HTML.UserLevelEnum)
                _userlevel = Value
            End Set
        End Property

        Public Property ModuleId() As Int32
            Get
                Return _ModuleId
            End Get
            Set(ByVal Value As Int32)
                _ModuleId = Value
            End Set
        End Property

        Public ReadOnly Property ShowWorkflowContent() As Boolean
            Get
                If _userlevel = UserLevelEnum.Workflow Then
                    Return True
                Else
                    Return False
                End If
            End Get
        End Property

        Public Property LocalResourceFile() As String
            Get
                Dim fileRoot As String

                If _localResourceFile = "" Then
                    ' fileRoot = Me.TemplateSourceDirectory & "/" & DotNetNuke.Services.Localization.Localization.LocalResourceDirectory & "/" & Me.ID
                    fileRoot = Me.TemplateSourceDirectory & "/" & DotNetNuke.Services.Localization.Localization.LocalResourceDirectory & "/" & "WF_Grid.ascx.resx"
                Else
                    fileRoot = _localResourceFile
                End If
                Return fileRoot
            End Get
            Set(ByVal Value As String)
                _localResourceFile = Value
            End Set
        End Property

#End Region

#Region " Delegates "

        Public Delegate Sub effority_WF_Grid_EventHandler(ByVal sender As Object, ByVal e As Int32)

#End Region

#Region " Events "

        Public Event ActiveVersionChanged As effority_WF_Grid_EventHandler
        Public Event ViewVersion As effority_WF_Grid_EventHandler
        Public Event DeleteVersion As effority_WF_Grid_EventHandler

#End Region

#Region " Event Handler "

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Try

                If Not IsPostBack Then

                    Me.Bind()

                End If

            Catch exc As Exception

                ProcessModuleLoadException(Me, exc)

            End Try

        End Sub

        Private Sub dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dg.ItemCommand

            Try

                If e.CommandName = "SetActive" Then

                    RaiseEvent ActiveVersionChanged(Me, CInt(e.CommandArgument))

                ElseIf e.CommandName = "Delete" Then

                    RaiseEvent DeleteVersion(Me, CInt(e.CommandArgument))

                ElseIf e.CommandName = "View" Then

                    RaiseEvent ViewVersion(Me, CInt(e.CommandArgument))

                End If

                Me.Bind()

            Catch exc As Exception

                ProcessModuleLoadException(Me, exc)

            End Try

        End Sub

        Private Sub dg_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg.ItemCreated

            Try

                If e.Item.ItemType = Web.UI.WebControls.ListItemType.Item Or e.Item.ItemType = Web.UI.WebControls.ListItemType.AlternatingItem Then

                    Dim ibView As System.Web.UI.WebControls.ImageButton
                    Dim ibSetActive As System.Web.UI.WebControls.ImageButton
                    Dim ibDelete As System.Web.UI.WebControls.ImageButton
                    Dim imActive As System.Web.UI.WebControls.Image

                    Dim info As effority.WF_HTML.HtmlTextInfo = CType(e.Item.DataItem, effority.WF_HTML.HtmlTextInfo)

                    If Not info Is Nothing Then

                        ibView = CType(e.Item.FindControl("ibView"), System.Web.UI.WebControls.ImageButton)
                        ibSetActive = CType(e.Item.FindControl("ibSetActive"), System.Web.UI.WebControls.ImageButton)
                        ibDelete = CType(e.Item.FindControl("ibDelete"), System.Web.UI.WebControls.ImageButton)
                        imActive = CType(e.Item.FindControl("imActive"), System.Web.UI.WebControls.Image)

                        If Not (Me.ShowWorkflowContent) Then
                            ibSetActive.Visible = False
                            ibDelete.Visible = False
                        End If


                        ' das ist ein (vollig) ungeeigneter Ort, da diese Informationen nicht im Viewstate gespeichert werder
                        ' und somit nicht beim nächsten Rountrip wiederhergestellt werden können

                        'ibView.CommandArgument = info.Id
                        'ibSetActive.CommandArgument = info.Id
                        'ibDelete.CommandArgument = info.Id

                        ibView.ImageUrl = "~/images/view.gif"
                        ibSetActive.ImageUrl = "~/images/icon_sitesettings_16px.gif"
                        ibDelete.ImageUrl = "~/images/delete.gif"

                        If info.Active Then
                            imActive.ImageUrl = "~/images/icon_hostsettings_16px.gif"
                            imActive.ToolTip = Localization.GetString("Active", Me.LocalResourceFile)
                        Else
                            imActive.ImageUrl = "~/images/icon_recyclebin_16px.gif"
                            imActive.ToolTip = Localization.GetString("NotActive", Me.LocalResourceFile)
                        End If

                        ibView.ToolTip = Localization.GetString("View", Me.LocalResourceFile)
                        ibSetActive.ToolTip = Localization.GetString("SetActive", Me.LocalResourceFile)
                        ibDelete.ToolTip = Localization.GetString("Delete", Me.LocalResourceFile)

                    End If

                End If

            Catch exc As Exception

                ProcessModuleLoadException(Me, exc)

            End Try

        End Sub

#End Region

#Region " Private Methods "

        Private Sub Bind()

            Dim cont As New effority.WF_HTML.HtmlTextController

            Dim liste As ArrayList = cont.GetHtmlTextAllVersions(Me.ModuleId)

            dg.DataSource = liste
            dg.DataBind()

        End Sub

#End Region

End Class

End Namespace



